package com.sunc.demo.order.service;

import com.sunc.demo.item.entity.Item;
import com.sunc.demo.order.entity.Order;
import com.sunc.demo.order.entity.OrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrderService {

    private final static Map<String,Order> MAP = new HashMap<>();

    static{
        Order order = new Order();
        order.setOrderId("123456789");
        order.setCreateDate(new Date());
        order.setUpdateDate(order.getCreateDate());
        order.setUserId(1L);


        List<OrderDetail> orderDetails = new ArrayList<>();

        Item item = new Item();
        item.setId(1L);
        orderDetails.add(new OrderDetail(order.getOrderId(),item));
        item = new Item();
        item.setId(2L);
        orderDetails.add(new OrderDetail(order.getOrderId(),item));

        order.setOrderDetails(orderDetails);

        MAP.put(order.getOrderId(),order);

    }

    @Autowired
    private LocalItemService itemService;

    public Order queryOrderById(String orderId){

        Order order = MAP.get(orderId);

        if (null == order){
            return null;
        }


        for (OrderDetail orderDetail : order.getOrderDetails()){
            Item item = itemService.queryItemById(orderDetail.getItem().getId());

            if (item == null){
                continue;
            }
            orderDetail.setItem(item);
        }

        return order;
    }

}
